package com.galaxy.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.validate.AddGroup;
import com.galaxy.common.core.validate.EditGroup;
import com.galaxy.common.log.annotation.Log;
import com.galaxy.common.log.enums.BusinessType;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.Product;
import com.galaxy.product.domain.bo.ProductBo;
import com.galaxy.product.domain.vo.ProductVo;
import com.galaxy.product.service.IProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理控制器
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {

    private final IProductService productService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
//    @SaCheckPermission("product:product:list")
    public TableDataInfo<ProductVo> list(ProductBo bo, PageQuery pageQuery) {
        return productService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping("/{id}")
//    @SaCheckPermission("product:product:query")
    public R<Product> getInfo(@PathVariable Long id) {
        return R.ok(productService.getInfo(id));
    }

    @GetMapping("byId/{id}")
//    @SaCheckPermission("product:product:query")
    public R<Product> getInfoById(@PathVariable Long id) {
        return R.ok(productService.getInfoById(id));
    }

    /**
     * 批量获取商品信息
     */
    @GetMapping("/batch")
//    @SaCheckPermission("product:product:query")
    public R<List<ProductVo>> getBatchInfo(@RequestParam List<Long> ids) {
        return R.ok(productService.getBatchInfo(ids));
    }

    /**
     * 新增商品
     */
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
//    @SaCheckPermission("product:product:add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductBo bo) {
        productService.add(bo);
        return R.ok();
    }

    /**
     * 修改商品
     */
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
//    @SaCheckPermission("product:product:update")
    public R<Void> update(@Validated(EditGroup.class) @RequestBody ProductBo bo) {
        productService.update(bo);
        return R.ok();
    }

    /**
     * 删除商品
     */
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @SaCheckPermission("product:product:delete")
    public R<Void> delete(@PathVariable Long id) {
        productService.delete(id);
        return R.ok();
    }

    /**
     * 批量删除商品
     */
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/{ids}")
    @SaCheckPermission("product:product:delete")
    public R<Void> deleteBatch(@PathVariable Long[] ids) {
        productService.deleteBatch(ids);
        return R.ok();
    }
}
